import java.util.ArrayList;

public class User {
    private String username;
    private ArrayList<String> shoppingHistory;

    public User(String username) {
        this.username = username;
        this.shoppingHistory = new ArrayList<>();
    }
    private String password;
    private boolean locked;
    private int loginAttempts;
    private String email;
    // Constructor
    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
        this.locked = false;
        this.loginAttempts = 0;
        this.shoppingHistory = new ArrayList<>();
    }
    public ArrayList<String> getShoppingHistory() {
        return shoppingHistory;
    }

    // Getters and Setters
    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isLocked() {
        return locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    // Login method
    public boolean login(String enteredPassword) {
        if (locked) {
            System.out.println("账户已锁定，请联系客服解锁！");
            return false;
        }
        if (this.password.equals(enteredPassword)) {
            loginAttempts = 0; // Reset login attempts on successful login
            return true;
        } else {
            loginAttempts++;
            if (loginAttempts >= 5) {
                locked = true; // Lock the account after 5 failed attempts
                System.out.println("密码连续输入错误5次，账户已锁定！");
            } else {
                System.out.println("密码错误，还剩余" + (5 - loginAttempts) + "次尝试机会。");
            }
            return false;
        }
    }

    // Add to shopping history
    public void addToShoppingHistory(String item) {
        shoppingHistory.add(item);
    }

    // View shopping history
    public void viewShoppingHistory() {
        System.out.println("购物历史记录:");
        for (String item : shoppingHistory) {
            System.out.println(item);
        }
    }
}
